package com.baichuan.zntx.handler;

import com.baichuan.zntx.domain.LoraMessage;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.util.List;

/**
 * @author mazhanliang
 */
public class LoraJsonDecoder  extends MessageToMessageDecoder<ByteBuf> {
    private final ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
        String jsonString = msg.toString(io.netty.util.CharsetUtil.UTF_8);
        LoraMessage jsonObject = objectMapper.readValue(jsonString, LoraMessage.class);
        if(jsonObject.crcCheck()){
            out.add(jsonObject);
        }else{
            throw new Exception("CRC check failed");
        }
    }
}
